package com.zhong.thesimpleconnection.action;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.design.internal.NavigationMenuView;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.easeui.ui.EaseBaseFragment;
import com.hyphenate.easeui.ui.EaseContactListFragment;
import com.hyphenate.easeui.ui.EaseConversationListFragment;
import com.zhong.bmob.model.User;
import com.zhong.thesimpleconnection.R;
import com.zhong.thesimpleconnection.adapter.CommenFragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, NavigationView.OnNavigationItemSelectedListener {

    private ImageView sweep;
    private ImageView message;
    private ImageView getLeft;
    private ImageView userIcon;
    private TextView userName;
    private DrawerLayout mDrawerLayout;
    private ImageView headerUserImg;
    private TextView headerUserName;
    private ImageView weatherImg;
    private TextView weatherName;
    private View headerView;
    private User mUserMessageAll;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_page);
        init();
        setData();
        setListener();
    }

    private void setListener() {
        sweep.setOnClickListener(this);
        message.setOnClickListener(this);
        getLeft.setOnClickListener(this);
        userIcon.setOnClickListener(this);
        userName.setOnClickListener(this);
        headerUserName.setOnClickListener(this);
        headerUserImg.setOnClickListener(this);
    }

    private void setData() {
        Bundle extras = getIntent().getExtras();
        mUserMessageAll = (User) extras.get("UserMessageAll");
        SharedPreferences user = getSharedPreferences("User", MODE_PRIVATE);
        String state = user.getString("State", "");
        if (state.equals("已登录")) {
            String realName;
            String imgPath;
            if (mUserMessageAll != null) {
                realName = mUserMessageAll.getRealName();
                imgPath = mUserMessageAll.getImg();
                if (realName != null && !"".equals(realName)) {
                    userName.setText(realName);
                    headerUserName.setText(realName);
                }else {
                    String username = mUserMessageAll.getUsername();
                    userName.setText(username);
                    headerUserName.setText(username);
                }
                if (imgPath != null && !"".equals(imgPath)){
                    Glide.with(this)
                            .load(imgPath)
                            .asBitmap()
                            .placeholder(R.mipmap.rpt)
                            .centerCrop()
                            .into(new BitmapImageViewTarget(userIcon){
                                @Override
                                protected void setResource(Bitmap resource) {
                                    RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(getResources(), resource);
                                    drawable.setCircular(true);
                                    userIcon.setImageDrawable(drawable);
                                }
                            });
                    Glide.with(this)
                            .load(imgPath)
                            .asBitmap()
                            .placeholder(R.drawable.ico_user_default)
                            .centerCrop()
                            .into(new BitmapImageViewTarget(headerUserImg){
                                @Override
                                protected void setResource(Bitmap resource) {
                                    RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(getResources(), resource);
                                    drawable.setCircular(true);
                                    headerUserImg.setImageDrawable(drawable);
                                }
                            });
                }
            }
        }
    }

    private void init() {
        sweep = (ImageView) findViewById(R.id.Main_Sweep);
        message = (ImageView) findViewById(R.id.Main_Message);
        getLeft = (ImageView) findViewById(R.id.main_get_left);
        userIcon = (ImageView) findViewById(R.id.main_person_icon);
        userName = (TextView) findViewById(R.id.main_load_person_name);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.activity_main_page);
        NavigationView navigationView = (NavigationView) findViewById(R.id.Main_Page_NavigationView);
        navigationView.setNavigationItemSelectedListener(this);
        setNavigationViewScrollGone(navigationView);
        headerView = navigationView.getHeaderView(0);
        headerUserImg = (ImageView) headerView.findViewById(R.id.Main_Page_NavigationView_Header_UserImg);
        headerUserName = (TextView) headerView.findViewById(R.id.Main_Page_NavigationView_Header_UserName);
        weatherImg = (ImageView) headerView.findViewById(R.id.weather_img);
        weatherName = (TextView) headerView.findViewById(R.id.weather_txt);
        Toolbar toolbar = (Toolbar) findViewById(R.id.Main_ToolBar);
        TabLayout tabLayout = (TabLayout) findViewById(R.id.Main_Tab_List);
        ViewPager viewPager = (ViewPager) findViewById(R.id.Main_ViewPager);
        setSupportActionBar(toolbar);
        List<EaseBaseFragment> fragments = new ArrayList<>();
        EaseConversationListFragment conversationListFragment = new EaseConversationListFragment();
        fragments.add(conversationListFragment);
        EaseContactListFragment contactListFragment = new EaseContactListFragment();
        fragments.add(contactListFragment);
        CommenFragmentPagerAdapter adapter = new CommenFragmentPagerAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        //设置item点击事件
        SharedPreferences preferences = getSharedPreferences("TalkWithOther", MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        contactListFragment.setContactListItemClickListener(new EaseContactListFragment.EaseContactListItemClickListener() {
            @Override
            public void onListItemClicked(EaseUser user) {
                edit.putString("OtherId", user.getUsername());
                edit.apply();
                startActivity(new Intent(MainActivity.this, ChatToChatActivity.class).putExtra(EaseConstant.EXTRA_USER_ID, user.getUsername()));
            }
        });
        conversationListFragment.setConversationListItemClickListener(new EaseConversationListFragment.EaseConversationListItemClickListener() {
            @Override
            public void onListItemClicked(EMConversation conversation) {
                edit.putString("OtherId", conversation.conversationId());
                edit.apply();
                startActivity(new Intent(MainActivity.this, ChatToChatActivity.class).putExtra(EaseConstant.EXTRA_USER_ID, conversation.conversationId()));
            }
        });
//        contactListFragment.setContactListItemClickListener(user -> startActivity(new Intent(MainActivity.this, ChatToChatActivity.class).putExtra(EaseConstant.EXTRA_USER_ID, user.getUsername())));
//        conversationListFragment.setConversationListItemClickListener(conversation -> startActivity(new Intent(MainActivity.this, ChatToChatActivity.class).putExtra(EaseConstant.EXTRA_USER_ID, conversation.conversationId())));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.Main_Sweep:
                break;
            case R.id.Main_Message:
                break;
            case R.id.main_get_left:
                mDrawerLayout.openDrawer(GravityCompat.START, true);
                break;
            case R.id.main_person_icon:
                mDrawerLayout.openDrawer(GravityCompat.START, true);
                break;
            case R.id.main_load_person_name:
                mDrawerLayout.openDrawer(GravityCompat.START, true);
                break;
            case R.id.Main_Page_NavigationView_Header_UserImg:
                Intent intent = new Intent(this, MyActivity.class);
                intent.putExtra("User", mUserMessageAll);
                startActivity(intent);
            case R.id.Main_Page_NavigationView_Header_UserName:
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.Main_Page_NavigationView_People:
                Intent intent = new Intent(this, AddFriendsActivity.class);
                startActivity(intent);
                break;
            case R.id.Main_Page_NavigationView_recommend:
                break;
            case R.id.Main_Page_NavigationView_Sacn:
                break;
            case R.id.Main_Page_NavigationView_setting:
                break;
            case R.id.Main_Page_NavigationView_theme:
                break;
        }
        mDrawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }

    private void setNavigationViewScrollGone(NavigationView navigationView){
        NavigationMenuView navigationMenuView = (NavigationMenuView) navigationView.getChildAt(0);
        if (navigationMenuView != null) {
            navigationMenuView.setVerticalScrollBarEnabled(false);
        }
    }
}
